import web
import os

urls = (
  '/login', 'login.login',
  '/css/(.*)', 'cssHandler',
  '/images/(.*)', 'imageHandler',
  '/(.*)', 'handler'
  )

app = web.application(urls, globals())

class index:
  def GET(self):
    render = web.template.render("templates/")
    return render.index()

class handler:
  def GET(self, name):
    render = web.template.render("templates/")
    if not name:
      return render.index()
    elif name == "login.html":
      return render.login()
    else:
      return web.notfound()

class cssHandler:
  def GET(self, name):
    if name in os.listdir("templates/css"):
      return open("templates/css/%s"%name, "r").read()
    else:
      return web.notfound()

class imageHandler:
    def GET(self,name):
        ext = name.split(".")[-1] # Gather extension

        cType = {
            "png":"images/png",
            "jpg":"image/jpeg",
            "gif":"image/gif",
            "ico":"image/x-icon"
            }

        if name in os.listdir('templates/images'):  # Security
            web.header("Content-Type", cType[ext]) # Set the Header
            return open('templates/images/%s'%name,"rb").read() # Notice 'rb' for reading images
        else:
            raise web.notfound()

if __name__ == "__main__":
  app.run()